package com.funtonow.jqdk.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.funtonow.jqdk.R;
import com.funtonow.jqdk.utils.ActivityManager;
import com.funtonow.jqdk.utils.LogUtil;
import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;

import butterknife.ButterKnife;

/**
 * @author Lenovo
 */
@SuppressLint("Registered")
public abstract class BaseActivity extends AppCompatActivity {

    protected static String TAG;
    protected Activity mContext;
    public ImageButton mIbReturn;
    public TextView mTvSave;
    public ImageButton ibNews;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        // 设置 Activity 屏幕方向
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 设置不自动弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        // 隐藏 ActionBar
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        // 设置 TAG
        TAG = this.getClass().getSimpleName();

        super.onCreate(savedInstanceState);
        this.mContext = this;


        // 设置布局
        setContentView(getView());

        // 绑定依赖注入框架
        ButterKnife.bind(this);

        onCreateActivity(savedInstanceState);
        // 将当前 Activity 推入栈中
        ActivityManager.getInstance().addActivity(this);
    }

    private View getView() {
        return getLayoutInflater().inflate(getLayoutRes(), null, true);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getLocalClassName());
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.i(this.getLocalClassName() + " *********  ");
        MobclickAgent.onPageStart(this.getLocalClassName());
        MobclickAgent.onResume(this);
    }

    /**
     * 初始化标题栏
     */
    public void initToolbar(String title) {
        mIbReturn = findViewById(R.id.ib_back);
        mTvSave = findViewById(R.id.tv_save);
        ibNews = findViewById(R.id.ib_news);
        TextView mTvTitle = findViewById(R.id.tv_title);
        // 设置标题
        if (!TextUtils.isEmpty(title)) {
            if (mTvTitle != null) {
                mTvTitle.setText(title);
            }
        }

        // 设置左侧图标
        mIbReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBack();
            }
        });
    }

    /**
     * 设置抽屉图片
     *
     * @param drawableId
     */
    public void setLeftDrawable(int drawableId) {
        if (mIbReturn != null) {
            mIbReturn.setImageDrawable(getResources().getDrawable(drawableId));
        }
    }


    /**
     * 设置抽屉图片
     *
     * @param drawableId
     */
    public void setRightDrawable(int drawableId) {
        if (ibNews != null) {
            ibNews.setImageDrawable(getResources().getDrawable(drawableId));
        }
    }


    /**
     * 返回
     */
    protected void onBack() {
        finish();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (null != this.getCurrentFocus()) {
            // 点击空白位置 隐藏软键盘
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getInstance().removeActivity(new WeakReference(this));
    }

    /**
     * 获取布局
     */
    public abstract int getLayoutRes();

    public abstract void onCreateActivity(@Nullable Bundle savedInstanceState);


}
